

using System;
using System.Collections.Specialized;
using System.Text;
using System.Xml.Serialization;

using JTC.MiniCommerce.Core;

namespace JTC.MiniCommerce.Store.Services {

  [Serializable()]
  public class ProviderSettings : IXmlSerializable {
  
    #region Constants
    
    private const string NAME = "Name";
    private const string TYPE = "Type";
    
    #endregion
    
    #region Member Variables

    private string _name;
    private string _type;
    private NameValueCollection _parameters;
    
    #endregion
    
    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="T:ProviderSettings"/> class.
    /// </summary>
    public ProviderSettings() {
      _parameters = new NameValueCollection();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:ProviderSettings"/> class.
    /// </summary>
    /// <param name="name">The name.</param>
    /// <param name="type">The type.</param>
    public ProviderSettings(string name, string type) {
      Validator.ValidateStringArgumentIsNotNullOrEmptyString(name, NAME);
      Validator.ValidateStringArgumentIsNotNullOrEmptyString(type, TYPE);
      _parameters = new NameValueCollection();
      _name = name;
      _type = type;
    }
    
    #endregion
    
    #region Properties

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [XmlAttribute]
    public string Name {
      get {
        return _name;
      }
      set {
        _name = value;
      }
    }

    /// <summary>
    /// Gets or sets the type.
    /// </summary>
    /// <value>The type.</value>
    [XmlAttribute]
    public string Type {
      get {
        return _type;
      }
      set {
        _type = value;
      }
    }

    /// <summary>
    /// Gets or sets the parameters.
    /// </summary>
    /// <value>The parameters.</value>
    [XmlIgnore]
    public NameValueCollection Parameters {
      get {
        return _parameters;
      }
      set {
        _parameters = value;
      }
    }

    /// <summary>
    /// Gets the arguments.
    /// </summary>
    /// <value>The arguments.</value>
    public object[] Arguments {
      get {
        object[] args = new object[_parameters.Keys.Count];
        for (int i = 0; i < _parameters.Keys.Count; i++) {
          args[i] = _parameters[i];
        }
        return args;
      }
    }
    
    #endregion

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema() {
     return null;
    }

    public void ReadXml(System.Xml.XmlReader reader) {
      _name = reader.GetAttribute(NAME);
      _type = reader.GetAttribute(TYPE);
      while (reader.MoveToNextAttribute())
        if(reader.Name != NAME && reader.Name != TYPE)
          _parameters.Add(reader.Name, reader.Value);
      reader.Read();
    }

    public void WriteXml(System.Xml.XmlWriter writer) {
      writer.WriteAttributeString(NAME, _name);
      writer.WriteAttributeString(TYPE, _type);
      foreach (string key in _parameters.Keys) {
        writer.WriteAttributeString(key, _parameters[key]);
      }
    }

    #endregion
    
  }
}
